package com.zy.web.demo;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;

public class newDuoxiancheng {

    private static List<Integer> list = new LinkedList<>();

    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10,
            10000,
            100L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(50),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
    );

    private static String Method(int i){
       return String.valueOf(i);
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 1.创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        // 2.定义CompletionService
        CompletionService<String> service = new ExecutorCompletionService<>(threadPool);
        // 3.添加任务
        for (int i = 0; i < 10; i++) {
            // lambda写法
            service.submit(() -> "线程：" + Thread.currentThread().getName() + "执行完成");
        }
        // 4.获取结果
        for (int i = 0; i <= 10; i++) {
            System.out.println(service.take().get());
        }
        // 关闭线程池
        executorService.shutdown();
    }

}
